package com.zzty.serviceimpl;

import com.zzty.common.DBUtil;
import com.zzty.dao.SelfExamDao;
import com.zzty.daoimpl.SelfExamDaoImpl;
import com.zzty.entity.Question;
import com.zzty.entity.SelfExam;
import com.zzty.entity.SelfQuestion;
import com.zzty.entity.SelfStudentHomework;
import com.zzty.service.SelfExamService;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SelfExamServiceImpl implements SelfExamService {


    public String startSelfExam(int userId) {
        System.out.println("竞赛开始");
        SelfExamDao selfExamDaoImpl=new SelfExamDaoImpl();
        Boolean checkLogIn =selfExamDaoImpl.checkLogIn(userId);
        System.out.println("之前是否登陆："+checkLogIn);
        String questionIds=null;
        if(checkLogIn==false){
        questionIds=selfExamDaoImpl.addSelfTransaction(userId);
        }else {
            System.out.println("已开始竞赛");
        }
        System.out.println("service questionIds :"+questionIds);
        return questionIds;
    }

    @Override
    public void addStartTransaction(Integer selfExamId, Integer userId,String nowDate) {
        SelfExamDao selfExamDao=new SelfExamDaoImpl();
        selfExamDao.addStartTransaction(selfExamId,userId,nowDate);
    }

    @Override
    public Boolean endSelfExam(Integer userId, String answerUrl) {
       SelfExamDao selfExamDao=new SelfExamDaoImpl();
       Boolean flag=selfExamDao.commit(userId,answerUrl);
       return flag;
    }


    public List historyQuestion() {
        SelfExamDaoImpl selfExamDao=new SelfExamDaoImpl();
        List<SelfQuestion> questions=null;
        if(selfExamDao.searchSelfQuestion()==null){
            return null;
        }else {
            questions=selfExamDao.searchSelfQuestion();
        }
        return questions;
    }

    @Override
    public String searchAnswerUrlById(int id) {
        return null;
    }

    public List getSelfExamToTable(){
        SelfExamDao sed = new SelfExamDaoImpl();
        List<SelfExam> selfExams=null;
        if(sed.getSelfExamToTable()==null){
            return null;
        }else{
            selfExams=sed.getSelfExamToTable();
        }
        return selfExams;
    }

    @Override
    public Integer getUserIdFromTransaction(Integer transactionId) {
        SelfExamDao selfExamDao=new SelfExamDaoImpl();
        Integer userId=selfExamDao.getUserIdFromTransaction(transactionId);
        return userId;
    }

    @Override
    public Boolean setWinner(String date, Integer winnerId) {
        SelfExamDao selfExamDao=new SelfExamDaoImpl();
        Boolean flag= selfExamDao.setWinner(date,winnerId);
        return  flag;
    }

    @Override
    public String getQuestionByDate(String date) {
        SelfExamDao selfExamDao=new SelfExamDaoImpl();
        String questionIds=selfExamDao.getQuestionByDate(date);
        return questionIds;
    }

    @Override
    public Integer getSelfExamIdDate(String date,Integer clazzId) {
        SelfExamDao selfExamDao=new SelfExamDaoImpl();
        Integer selfExamId=selfExamDao.getSelfExamIdDate(date,clazzId);
        return selfExamId;
    }

    @Override
    public Boolean checkLogin(Integer selfExamId, Integer userId) {
        SelfExamDao selfExamDao=new SelfExamDaoImpl();
        Boolean flag=selfExamDao.checkLogIn(selfExamId,userId);
        return  flag;
    }

    @Override
    public Boolean checkCommit(Integer selfExamId, Integer userId) {
        SelfExamDao selfExamDao=new SelfExamDaoImpl();
        Boolean flag=selfExamDao.checkCommit(selfExamId,userId);
        return  flag;
    }

    @Override
    public void setGrade(Integer transactionId, Integer score) {
        SelfExamDao selfExamDao=new SelfExamDaoImpl();
        selfExamDao.setGrade(transactionId,score);
    }

    //public static void main(String args[]){
//    String date="2018-06-09";
//    SelfExamDao selfExamDao=new SelfExamDaoImpl();
//    String questionIds=selfExamDao.getQuestionByDate(date);
//    System.out.println(questionIds);
//
//}
    public void historyAnswer() {

    }


    public void checkWork() {

    }

    @Override
    public List getContentByDate(String date) {
        SelfExamDao selfExamDao=new SelfExamDaoImpl();
        List<SelfStudentHomework> studentHomeworkLIst=selfExamDao.getContentByDate(date);
        return studentHomeworkLIst;
    }
}
